#include "stdafx.h"
#include "CRectangle.h"


CRectangle::CRectangle(void)
{
}

CRectangle::CRectangle(CPoint pBegin, CPoint pEnd, COLORREF cPen)
{
	this->m_pBegin	= pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];

	this->m_cPenColor = cPen;
	m_brushColor.CreateSolidBrush(RGB(255, 255, 255));
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
}

CRectangle::CRectangle (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType, INT m_nRotate)
{
	this->m_nRotate = m_nRotate;
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
	m_brushColor.CreateSolidBrush(RGB(255,255,255));
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
}


BOOL CRectangle::Draw(CDC *cDC)
{
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	CBrush brushColor (m_clrBack);
	CBrush *OldBrush = cDC->SelectObject(&brushColor);

	this->setCoordinate();
	cDC->Polygon(m_pCoordinates, NUM_OF_ANGLE);

	cDC->SelectObject(pOldPen);
	cPen.DeleteObject();
	cDC->SelectObject (OldBrush);
	brushColor.DeleteObject();
	return	FALSE;
}

void CRectangle::setCoordinate()
{
	CRect cRect(m_pBegin, m_pEnd);
	m_pCoordinates[0]  = CPoint (cRect.left, cRect.top);
	m_pCoordinates[1]  = CPoint (cRect.left, cRect.bottom);
	m_pCoordinates[2]  = CPoint (cRect.right, cRect.bottom);
	m_pCoordinates[3]  = CPoint (cRect.right, cRect.top);
	m_pMax = m_pMin = m_pBegin;
	for(int i = 0; i < m_nAngle; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}

}
CRectangle::~CRectangle(void)
{
}

void CRectangle::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color << m_nRotate;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color >> m_nRotate;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}